package com.qzsoft.zhjd.materiel.modules.materielgas.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qzsoft.zhjd.common.annotation.ZhjdDataFilter;
import com.qzsoft.zhjd.common.components.BaseServiceComponent;
import com.qzsoft.zhjd.common.enums.BizCode;
import com.qzsoft.zhjd.common.utils.RenExceptionUtils;
import com.qzsoft.zhjd.materiel.modules.materielgas.dto.MaterielGasListDto;
import com.qzsoft.zhjd.materiel.modules.materielgas.entity.MaterielGasEntity;
import com.qzsoft.zhjd.materiel.modules.materielgas.mapper.MaterielGasMapper;
import com.qzsoft.zhjd.materiel.modules.materielgas.service.MaterielGasService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Date;
import java.util.Map;

/**
 * @ClassName MaterielGasService
 * @Description 液化天然气业务接口实现
 * @author linhuawei
 * @since 2022-04-05
 */
@Service
public class MaterielGasServiceImpl extends ServiceImpl<MaterielGasMapper, MaterielGasEntity>
        implements MaterielGasService {

    @Autowired
    private BaseServiceComponent baseServiceComponent;

    @Override
    @ZhjdDataFilter(tableAlias = "t1")
    public IPage<MaterielGasListDto> pageDto(Map<String, Object> params) {
        IPage<MaterielGasListDto> page = baseServiceComponent.getPage(params, MaterielGasListDto.class);
        return baseMapper.queryDto(page, params);
    }

    @Override
    public boolean save(MaterielGasEntity entity) {
        // 如果不添加抄表时间，默认服务器时间
        if (null == entity.getReadingTime()) {
            entity.setReadingTime(new Date());
        }
        getPreNum(entity);
        return super.save(entity);
    }

    @Override
    public boolean updateById(MaterielGasEntity entity) {
        // 如果不添加抄表时间，默认服务器时间
        if (null == entity.getReadingTime()) {
            entity.setReadingTime(new Date());
        }
        getPreNum(entity);
        return super.updateById(entity);
    }

    /**
     * @Title getPreNum
     * @Description 上次抄表记录
     */
    private void getPreNum(MaterielGasEntity entity) {
        QueryWrapper<MaterielGasEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("base_code", entity.getBaseCode());
        queryWrapper.lt("reading_time", entity.getReadingTime());
        queryWrapper.orderByDesc("reading_time");
        queryWrapper.last(" LIMIT 0,1 ");
        MaterielGasEntity temp = baseMapper.selectOne(queryWrapper);
        if (null == temp) {
            entity.setPreGasNum(BigDecimal.ZERO);
            entity.setUseGasNum(entity.getGasNum());
        } else {
            BigDecimal preGasNum = temp.getGasNum();
            BigDecimal gasNum = entity.getGasNum();
            if (null == preGasNum || -1 == preGasNum.compareTo(gasNum)) {
                RenExceptionUtils.throwEx(BizCode.PARAMS_ERROR, "本次抄表值不可小于上次抄表值");
            }
            BigDecimal useGasNum = gasNum.subtract(preGasNum);
            entity.setPreGasNum(preGasNum);
            entity.setUseGasNum(useGasNum);
        }
    }

}
